<?php
/* [DoYouHaoBaby!] (C)Dianniu From 2010.
   Gtk html窗口($)*/

!defined('DYHB_PATH') && exit;

class GtkHtmlWidget extends GtkHtml{

	private $_arrObjects=array();
	private $_sTempDir='';

	public function __construct(){
		$arrArgs=func_get_args();
		call_user_func_array(array($this,'__construct'),$arrArgs);
		$this->connect('object-requested',array($this,'onObjectRequested'));
	}

	public function locate($sUrl){
		$sHtmlString=file_get_contents($sUrl);
		$this->loadFromString($sHtmlString);
	}

	public function loadFromString($sHtmlString){
		$sRegExp='|<img([^>]*)/?>|i';// 将 <img> 替换成 <OBJECT>,$sHtmlString=str_ireplace('<img ','<object ',$sHtmlString);
		$arrResult=array();
		if(preg_match_all($sRegExp,$sHtmlString,$arrResult)){
			foreach($arrResult[0] as $nIdx=>$sSource){
				$sHtmlString=str_replace($sSource,"<OBJECT type='image' classid=\"img:{$nIdx}{$arrResult[1][$nIdx]}\"></OBJECT>",$sHtmlString);
			}
		}
		$sRegExp="|<object[^>]*></object>|i";// 整理所有 <OBJECT>
		$this->_arrObjects=array(); 
		if(preg_match_all($sRegExp,$sHtmlString,$arrResult)){
			foreach($arrResult[0] as $sSource){
				$this->_arrObjects[]=$sSource;
			}
		}
		$this->load_from_string($sHtmlString);// 载入 修改过的 html
	}

	public function onObjectRequested(GtkHtml $wndGtkHtml, $wndGtkHtmlEmbedded){
		$sObjectSource=array_shift($this->_arrObjects);
		$sType=strtolower(self::getAttributeFromHtmlNode($sObjectSource,'type'));
		if($sType!==null){
			switch($sType){
				case 'image' :
					return $this->onLoadImage($sObjectSource,$wndGtkHtmlEmbedded,$wndGtkHtml);
				default :
					return false;
			}
		}
	}

	protected function onLoadImage($sHtmlTag,GtkHtmlEmbedded $wndGtkHtmlEmbedded){
		$sSrc=self::getAttributeFromHtmlNode($sHtmlTag,'src');
		if($sSrc===null){
			return false;
		}
		if(preg_match('|^http://|i',$sSrc)){// 打开非 本地文件
			if(!$sImage=file_get_contents($sSrc)){
				return false;// 显示 红X
			}
			else{
				$sSrc=$this->_sTempDir.time().'_'.rand(0,99999);
				file_put_contents($sSrc,$sImage);
				$bTempPath=$sSrc;
			}
		}
		else {
			$bTempPath='';
		}
		$arrImageInfo=getimagesize($sSrc);// 图片信息
		if($arrImageInfo===false){
			return false;
		}
		try{// 载入图像
			$oBitmap=GdkPixbuf::new_from_file($sSrc);
		}
		catch(Exception $oE){
			$oE; 
			return false;
		}
		$sWidth=self::getAttributeFromHtmlNode($sHtmlTag,'width');// 取得图片尺寸
		$sHeight=self::getAttributeFromHtmlNode($sHtmlTag,'height');
		$nWidth =($sWidth===null)? $arrImageInfo[0]: intval($sWidth);
		$nHeight =($sHeight===null)? $arrImageInfo[1]: intval($sHeight);
		$oImage=GtkImage::new_from_pixbuf($oBitmap);// 创建 图像窗体
		$wndGtkHtmlEmbedded->add($oImage);
        $wndGtkHtmlEmbedded->set_size_request($nWidth, $nHeight);
		$oImage->show();
		if($bTempPath){// 清除 临时文件
			unlink($bTempPath);
		}
		return true;
	}

	static public function getAttributeFromHtmlNode($sHTMLNodeString,$sAttributeName){
		$arrRegExps=array(
			"/{$sAttributeName}=[\"']([^\"']+)[\"']/i",// 使用 引号
			"/{$sAttributeName}=([^\t ]+)/i"// 无引号
		);
		$arrResult=array();
		foreach($arrRegExps as $sRegExp){
			if(preg_match($sRegExp,$sHTMLNodeString,$arrResult)){
				return $arrResult[1];
			}
		}
		return null;
	}
	
}
